package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* CloudDownloadView
* */
class CloudDownloadView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            color = context.resources.getColor(R.color.color_E60C3F)
        }
    }

    private val mPaint1 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.ROUND
            strokeJoin = Paint.Join.ROUND
            strokeWidth = context.resources.getDimension(R.dimen.dp_5)
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_ee000000)
        }
    }

    private val mPath by lazy {
        Path()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            translate(cx, cy + cy / 4)

            drawCircle(
                0F,
                -cy / 2,
                cx / 2,
                mPaint0
            )
            drawCircle(
                -cx / 2,
                0F,
                cx / 2,
                mPaint0
            )
            drawCircle(
                cx / 2,
                0F,
                cx / 2,
                mPaint0
            )

            mPath.apply {
                reset()
                moveTo(0F, -cy / 2)
                lineTo(-cx / 2, cy / 2)
                lineTo(cx / 2, cy / 2)
                close()
            }
            drawPath(mPath, mPaint0)

            save()
            translate(0F,(cy / 4 - cy * 5 / 8) / 4)
            mPath.apply {
                reset()
                moveTo(0F, -cy * 3 / 8)
                lineTo(0F, cy / 4)
                lineTo(-cx / 4, 0F)
                moveTo(0F, cy / 4)
                lineTo(cx / 4, 0F)
            }
            drawPath(mPath, mPaint1)
            restore()
        }
    }
}